package com.ruiyin.controller;

import com.ruiyin.entity.AdminAccount;
import com.ruiyin.entity.App;
import com.ruiyin.entity.Valid;
import com.ruiyin.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("admin/api")
public class AdminController extends BaseController {
    @Autowired
    private AdminService service;

    @RequestMapping("reg")
    @ResponseBody
    public Object reg(AdminAccount account) {
        try {
           return service.reg(account)==0?sendSuccess(0, "注册成功"):sendFailed(1, "注册失败,账号已存在!");
        } catch (Exception e) {
            return sendFailed(1, "注册失败");
        }
    }

    @RequestMapping("login")
    @ResponseBody
    public Object login(AdminAccount account) {
        try {
           return service.login(account)==0?sendSuccess(0, "登陆成功"):sendFailed(1, "登录失败");
        } catch (Exception e) {
            return sendFailed(1, "登录失败");
        }
    }

    @RequestMapping("act")
    @ResponseBody
    public Object act(@Param("imei") String imei, @Param("type") Integer type,@Param("flag")String flag) {
        if (StringUtils.isBlank(imei) && StringUtils.isBlank(flag)) {
            return sendFailed(CODES,"非法请求");
        }
        if (service.queryIMEI(imei) == CODE1) {
            return sendFailed(CODES,"账号不存在");
        } else if (service.queryIMEI(imei) == CODE2) {
            try {
                service.activity(imei, type,flag);
            } catch (Exception e) {
                return sendFailed(CODE2, "账号激活失败");
            }
        } else {
            return sendFailed(CODES, "账号存在异常,请联系管理员");
        }
        return sendSuccess(0,"ok");
    }

    @RequestMapping("queryCheck")
    @ResponseBody
    public Object queryCheck(AdminAccount account) {
        return sendSuccess(0,"成功",service.queryUser(account));
    }

    @RequestMapping(value = "queryInfo",method = RequestMethod.GET)
    @ResponseBody
    public Object queryInfo() {
        return sendSuccess(0, "ok", service.findAll());
    }

    @RequestMapping("disc")
    @ResponseBody
    public Object disc(String imei,String status){
        if(StringUtils.isBlank(imei)){
            return sendFailed("非法访问");
        }
        try {
            service.updateStatus(imei,status);
        } catch (Exception e) {
            return sendFailed("修改失败,服务器故障");
        }
        return sendSuccess(0,"禁用账号成功");
    }

    @RequestMapping("updateApp")
    @ResponseBody
    public Object updateApp(App app){
        if(org.springframework.util.StringUtils.isEmpty(app)){
            return sendFailed(-1,"非法参数");
        }
        try {
            service.insertUrl(app);
        } catch (Exception e) {
            return sendFailed(-1,"更新失败!");
        }
        return sendSuccess(0,"更新地址成功");
    }
}



